package com.five.system.controller;

import com.five.basic.result.PageList;
import com.five.exception.BusinessException;
import com.five.pojo.User;
import com.five.result.Result;
import com.five.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/queryAll")
    public Result queryAll() {
        try {
            List<User> users = userService.queryAll();
            System.out.println(users);
            return Result.success(users);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @PostMapping("/queryById")
    public Result queryById(Long id) {
        try {
            User user = userService.queryById(id);
            return Result.success(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody User user) {
        try {
            PageList<User> pageList = userService.queryPage(user);
            return Result.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 根据LogininfoId查询员工信息
     * @param loginInfoId
     * @return
     */
    @PostMapping("queryByLoginInfoId/{loginInfoId}")
    public Result<User> queryByLoginInfoId(@PathVariable Long loginInfoId){
        try {
            User user = userService.queryByLoginInfoId(loginInfoId);
            return Result.success(user);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 新增员工信息
     * @param user
     * @return
     */
    @PostMapping("insertUser")
    public Result insertUser(@RequestBody User user){
        try {
            userService.insertUser(user);
            return Result.success("新增员工成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 更新员工信息
     * @param user
     * @return
     */
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user){
        try {
            userService.updateUser(user);
            return Result.success("更新员工成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     *根据账号查询员工信息
     * @param
     * @return
     */
    @PostMapping("queryByAccount/{account}")
    public Result<User> queryByAccount(@PathVariable String account){
        try {
            User user = userService.queryByAccount(account);
            return Result.success(user);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        try {
            userService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        try {
            userService.deleteBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }
}
